package com.project.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.project.bean.*;
import com.project.service.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.List;

@RestController
@RequestMapping("goods")
@Api(tags = "库存商品控制层")

public class GoodsController {
    @Autowired
    private IBiaoShuService biaoShuService;
    @Autowired
    private IInRecordService inRecordService;
    @Autowired
    private IGoodsService goodsService;
    @Autowired
    private IGoodsTotalService goodsTotalService;
    @Autowired
    private IOrderInfoService orderInfoService;
    @Autowired
    private IOrderService orderService;
    @Autowired
    private IOutRecordService outRecordService;

    @ApiOperation(value = "动态条件查询")
    @GetMapping("findByItem")
    //拉取信息
   public Page<GoodsBean> findByItem(Integer pageNum,
                                     String goodsName,
                                     String goodsType,
                                     String goodsStatus){
       return goodsService.findByItem(pageNum, goodsName, goodsType, goodsStatus);
   }

   //入库
   @ApiOperation(value = "入库")
   @PostMapping("goodsIn/{biaoShuId}/{goodsName}/{goodsSize}/{goodsColor}/{type}/{createAddress}/{createDate}/{num}")
   public String goodsIn(@PathVariable("biaoShuId") Integer biaoShuId,
                         @PathVariable("goodsName") String goodsName,
                         @PathVariable("goodsSize") String goodsSize,
                         @PathVariable("goodsColor") String goodsColor,
                         @PathVariable("type") String type,
                         @PathVariable("createAddress") String createAddress,
                         @PathVariable("createDate") LocalDate createDate,
                         @PathVariable("num") Integer num){
       //修改标书当前入库数
       biaoShuService.updateInNum(biaoShuId,num);

       BiaoShuBean biaoShuBean = biaoShuService.findById(biaoShuId);
       //修改标书状态
       if (biaoShuBean.getInNum() == biaoShuBean.getAllNum()){
           biaoShuService.updateStatus(biaoShuId);
       }
       //添加入库记录
       InRecordBean inRecordBean = new InRecordBean(biaoShuBean.getGoodsId(),num,createDate,biaoShuId);
       inRecordService.add(inRecordBean);

       //生成批次号
       String groupId = biaoShuBean.getSupplierName()+"_"+goodsName+"_"+goodsSize+"_"+LocalDate.now()+"_"+Math.random();
       //添加库存商品
       goodsService.add(new GoodsBean(biaoShuBean.getGoodsId(), goodsName,goodsSize,
               goodsColor,createAddress,type,createDate,num,groupId));

       //更新库存总览
       GoodsTotalBean goodsTotalBean = goodsTotalService.findByItem(goodsName, goodsSize, goodsColor);
       if (goodsTotalBean==null){
           goodsTotalService.add(new GoodsTotalBean(biaoShuBean.getGoodsId(),goodsName,num,10,goodsSize,goodsColor,type));
       }else {
           goodsTotalService.update(goodsTotalBean.getId(),num);
       }
       return "ok";
   }



   //出库
   @ApiOperation(value = "出库")
   @PutMapping("goodsOut/{orderId}")
   public String goodsOut(@PathVariable("orderId") Integer orderId){
       //查询订单的订单明细
       List<OrderInfoBean> orderInfoBeanList = orderInfoService.findByOrderId(orderId);
       for (OrderInfoBean orderInfoBean:orderInfoBeanList
            ) {
           //减库存商品数量
           goodsService.updateNum(orderInfoBean.getGoodsId(),0-orderInfoBean.getNum());
           //减少库存总览是在生成订单时减少

           //生成出库记录
           outRecordService.add(new OutRecordBean(orderId,orderInfoBean.getGoodsId(),orderInfoBean.getNum()));
       }
       //修改订单状态
       orderService.updateStatus(orderId,"已发货");
       return "ok";
   }


}
